package com.xh.demo.commons.asserts;


import com.xh.demo.commons.enums.MessageEnum;
import com.xh.demo.commons.exception.BusinessException;
import com.xh.demo.commons.utils.ValidUtil;

import java.util.Collection;
import java.util.Map;

/**
* @apiNote 自定义断言类
* @author wen
* @since 2021/12/11
*/
public class Assert {

    public static void isFalse(boolean expression, MessageEnum messageEnum) throws BusinessException {
        isFalse(expression, messageEnum.getCode(), messageEnum.getMsg());
    }

    public static void isFalse(boolean expression, String msg) throws BusinessException {
        isFalse(expression, MessageEnum.PARAMETER_VERIFICATION_ERROR.getCode(), msg);
    }

    public static void isFalse(boolean expression, int code, String msg) throws BusinessException {
        if (expression) {
            throw new BusinessException(code, msg);
        }
    }

    public static void notNull(CharSequence parameter, String msg) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), msg);
    }

    public static void notEmpty(Collection<?> parameter, String msg) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), msg);
    }

    public static void notEmpty(Object[] parameter, String msg) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), msg);
    }

    public static void notEmpty(Map<?, ?> parameter, String msg) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), msg);
    }

    public static void notNull(Object parameter, String msg) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), msg);
    }

    public static void notNull(CharSequence parameter, MessageEnum messageEnum) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), messageEnum);
    }

    public static void notEmpty(Collection<?> parameter, MessageEnum messageEnum) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), messageEnum);
    }

    public static void notEmpty(Object[] parameter, MessageEnum messageEnum) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), messageEnum);
    }

    public static void notEmpty(Map<?, ?> parameter, MessageEnum messageEnum) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), messageEnum);
    }

    public static void notNull(Object parameter, MessageEnum messageEnum) throws BusinessException {
        isFalse(ValidUtil.isNull(parameter), messageEnum);
    }

}
